#!/bin/bash

# If it is the first time to use this script, sudo echo "# laoD hosts" >> /etc/hosts

host_tmp=/tmp/hosts
wget https://raw.githubusercontent.com/racaljk/hosts/master/hosts -qO $host_tmp
line=$(sed -n "/Modified hosts start/=" $host_tmp)
line=$(($line - 1))
sed -i -e "1,${line}d" $host_tmp

hosts_new=/tmp/hosts_new
cp /etc/hosts $hosts_new
line=$(sed -n "/laoD hosts/=" $hosts_new)
if [ "$line" = "" ];then
    echo "# laoD hosts" >> $hosts_new
    line=$(sed -n "/laoD hosts/=" $hosts_new)
fi
[ -z "$line" ] && echo pls input "laoD hosts" to /etc/hosts && exit -1
line=$(($line + 1))
sed -i -e "${line},\$d" $hosts_new

echo new file /tmp/hosts_new
cat $host_tmp >> $hosts_new
sudo cp $hosts_new /etc/hosts

rm -rf $host_tmp $hosts_new

